package org.example.config;

import cn.hutool.core.thread.NamedThreadFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author yangzhenyu
 * @version 1.0
 * @description:
 * @date 2023/4/27 17:12
 */
@Configuration
@EnableAsync
public class ThreadPoolTaskExecutorConfig {
    @Autowired
    private TaskParam taskParam;
    @Bean("jobThreadPoolExecutor")
    public ThreadPoolExecutor taskExecutor() {
        int threads = Math.min(Runtime.getRuntime().availableProcessors() * 2, taskParam.getMaxThreads());

        return new ThreadPoolExecutor(threads, threads,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(taskParam.getWorkQueue()),
                new NamedThreadFactory("MigrationJob-", false));

    }
}
